package servelet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import JDBC.DBConn;

/**
 * Servlet implementation class AlarmTimeServelet
 */
@WebServlet("/AlarmTimeServelet")
public class AlarmTimeServelet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static String alarmClock;
	static List list=new ArrayList() ;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AlarmTimeServelet() {
        super();
    }
  
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String alarm = request.getParameter("alarmClock");
		System.out.println(alarm);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 alarmClock = "";
		 HttpSession session = request.getSession();
		 if(request.getParameter("alarmClock")!=null);
		 {
		 session.setAttribute("alarmClock",request.getParameter("alarmClock"));
		 }
		 alarmClock = (String)session.getAttribute("alarmClock");
		 System.out.println("--------闹钟启动---------");
		 list.add(alarmClock);
		 Timer timer = new Timer();
		 Statement st = null;
		 int rs;
		 Connection conn = null;
		 
//		 if(list.size()-1 == 1){
//			 timer.schedule(new TimerTask() {  
//				 Logger logger = Logger.getAnonymousLogger();
//		            public void run() {  
//			            	Date now = new Date(); 
//				    		SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");//yyyy/MM/dd
//				    		logger.info("闹钟2启动中");
//			    				if (time.format(now).toString().compareTo((String) list.get(1)) == 0){
//					    	 		this.cancel();
//			    					logger.info("闹钟2时间到");
//				    			}
//		            	 
//		    		}
//		        }, 1000,1000);
//		 }
//		 if(list.size()-1 == 2){
//			 timer.schedule(new TimerTask() {  
//				 Logger logger = Logger.getAnonymousLogger();
//		            public void run() {  
//			            	Date now = new Date(); 
//				    		SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");//yyyy/MM/dd
//				    		logger.info("闹钟3启动中");
//			    				if (time.format(now).toString().compareTo((String) list.get(list.size()-1)) == 0){
//					    	 		this.cancel();
//			    					logger.info("闹钟3时间到");
//				    			}
//		            	 
//		    		}
//		        }, 1000,1000);
//		 }
		 try{
			 conn = DBConn.getConn();
			 st = conn.createStatement();
			 if(list.size()-1 == 0){
				 timer.schedule(new TimerTask() {  
					 Logger logger = Logger.getAnonymousLogger();
			            public void run() {  
			            	Date now = new Date(); 
				    		SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");//yyyy/MM/dd
				    		logger.info("闹钟1启动中");
				    			if (time.format(now).toString().compareTo((String) list.get(0)) == 0){
					    	 		this.cancel();
			    					logger.info("闹钟1时间到");
				    			}
			    		}
			        }, 1000,1000);
			 }	
			 long start = System.currentTimeMillis(); 
		      for(int i = 0 ; i <1500000; i++){
		    	//随机生成充电站ID(01-10)
					 Random random = new Random();
					 int chargingStation = random.nextInt(10);
					 Format num = new DecimalFormat("00");
					 String chargingStationID = num.format(chargingStation);
					 //随机生成1-100
					 int rd = (int)(Math.random()*100);	
					 //随机生成3位数
					 int three = (int)(Math.random()*1000);
					 //分公司ID
					 int branchCompanyID = (int)(Math.random()*10);
					 //随机1-10
					 int number = (int)( Math.random()*10);
				/*	 String sql ="INSERT INTO chargingstationhistory (EquipmentID,"+
									 "ChargingStationID,"+
									 "ChargingStationName,"+
									"CardNo,"+
									 "CarNo,"+
									 "CarNumber,"+
									 "LineName,"+
									 "BranchCompany,"+
									 "StartChargingDTTM,"+
									 "EndChargingDTTM,"+
									 "StartSOC,"+
									 "ENDSOC,"+
									 "StartVoltage,"+
									 "EndVoltage,"+
									 "MaxCurrent,"+
									 "AveCurrent,"+
									 "StartAmmeterNo,"+
									 "EndAmmeterNo,"+
									 "TotalSOC,"+
									 "TotalChargingKWH,"+
									 "TotalAmount,"+
									 "TotalChargingDTTM,"+
									 "ConsumptionKWH,"+
									 "ConsumptionAmount,"+
									 "ServiceAmount,"+
									 "EndReason,"+
									 "SendFlag,"+
									 "CreateUser,"+
									 "CreateDTTM,"+
									 "UpdateUser,"+
									 "UpdateDTTM)" +
									 "VALUES ('48fecd99fd',"+chargingStationID+",'北京怀柔公交场站','09010680','京AR9673',"+rd+","
									 		     + "'H56路',"+branchCompanyID+",SYSDATE(),SYSDATE(),"+rd+","+rd+",'"+three+".8',"
									 				+ "'"+three+".8',"+number+","+number+","+rd+","+rd+","+rd+",'50.8',"+rd+","+three+",'"+three+".1',"+
											     ""+rd+","+rd+","+rd+",'0','hssy',SYSDATE(),'hssy',SYSDATE())";*/
					/* String sql="INSERT INTO chargingstationhistory ("
					 		+ "EquipmentID,"
					 		+ "ChargingStationID,"
					 		+ "ChargingStationName,"
					 		+ "CardNo,"
					 		+ "CarNo,"
					 		+ "CarNumber,"
					 		+ "LineName,"
					 		+ "BranchCompany,"
					 		+ "StartChargingDTTM,"
					 		+ "EndChargingDTTM,"
							+ "StartSOC,"
							+ "ENDSOC,"
							+ "StartVoltage,"
							+ "EndVoltage,"
							+ "StartAmmeterNo,"
							+ "EndAmmeterNo,"
							+ "TotalSOC,"
							+ "TotalChargingKWH,"
							+ "TotalChargingDTTM,"
							+ "EndReason,"
							+ "SendFlag,"
							+ "ResendFlag,"
							+ "DeleteFlag"
							+ "EquipmentFileFlag"
							+ "CarFileFlag"
							+ "CreateUser,"
							+ "CreateDTTM,"
							+ "UpdateUser,"
							+ "UpdateDTTM) "
							+ "VALUES ('0618c50ecd','014','西城区四新路44公交车站','08008955','0','00027374','0','0',SYSDATE(),SYSDATE(),"
							+ " '80','90','565.8','696.5','186',"
							+ "'210','10','24','200','手动停止','1','1','1','1','1','zhh',SYSDATE(),'zhh',SYSDATE())";*/
				String sql = "INSERT INTO chargingstationhistory (EquipmentID,"
						+"ChargingStationID,"
					 	+"	 ChargingStationName,"
					 	+"	 CardNo,"
					 	+"	 CarNo,"
					 	+"	 CarNumber,"
					 	+"	 LineName,"
					 	+"	 BranchCompany,"
					 	+"	 StartChargingDTTM,"
					 	+"	 EndChargingDTTM,"
						+"	 StartSOC,"
						+"	 ENDSOC,"
						+"	 StartVoltage,"
						+"	 EndVoltage,"
						+"	 StartAmmeterNo,"
						+"	 EndAmmeterNo,"
						+"	 TotalSOC,"
						+"	 TotalChargingKWH,"
						+"	 TotalChargingDTTM,"
						+"	 EndReason,"
						+"	 SendFlag,"
						+"	 ResendFlag,"
						+"	 DeleteFlag,"
						+"	 EquipmentFileFlag,"
						+"	 CarFileFlag,"
						+"	 CreateUser,"
						+"	 CreateDTTM,"
						+"	 UpdateUser,"
						+"	 UpdateDTTM) "
						+"	 VALUES ('0618c50ecd','014','西城区四新路44公交车站','08008955','0','00027374','0','0',SYSDATE(),SYSDATE(),"
						+"	  '80','90','565.8','696.5','186',"
						+"	 '210','10','24','200','手动停止','1','1','1','1','1','zhh',SYSDATE(),'zhh',SYSDATE());";
				 rs = st.executeUpdate(sql);	 
			 } 
		     long end = System.currentTimeMillis();
		     long resu = (start-end)/1000;
			 System.out.println("插入完成,耗时"+resu+"秒");
			 response.setContentType("text/html;charset=utf-8");
			 PrintWriter out = response.getWriter();
			 for(int i=0;i<list.size();i++){
				 out.println("闹钟已创建,时间设定为:"+list.get(i)+"<br>");
			 }
			 out.println("<a href='http://localhost:8080/AlarmTime'>添加闹钟</a>");
			 
//			 request.getRequestDispatcher("time.html"). forward(request, response);
		 } catch (SQLException e) {  
		            e.printStackTrace();  
		        } finally {  
		            // 最终关闭连接  
		            DBConn.close(conn);  
		        }  
		 }

	}
